package com.hdoit.apollo.logical.shop.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.hdoit.apollo.logical.shop.dao.IStaffDao;
import com.hdoit.apollo.logical.shop.model.Customer;
import com.hdoit.apollo.logical.shop.model.Shop;
import com.hdoit.apollo.logical.shop.model.Staff;
import com.hdoit.apollo.logical.shop.service.ICustomerService;
import com.hdoit.apollo.logical.shop.service.IShopService;
import com.hdoit.apollo.logical.shop.service.IStaffService;
import com.tennetcn.authority.common.enums.SexStatus;
import com.tennetcn.authority.common.message.AuthorityLoginUser;
import com.tennetcn.authority.logical.model.Role;
import com.tennetcn.authority.logical.model.RoleLoginUser;
import com.tennetcn.authority.logical.service.ILoginUserService;
import com.tennetcn.authority.logical.service.IRoleLoginUserService;
import com.tennetcn.core.common.enums.pub.YesOrNoInteger;
import com.tennetcn.core.common.utils.ChineseUtils;
import com.tennetcn.core.common.utils.CommonUtils;
import com.tennetcn.core.common.utils.DateUtils;
import com.tennetcn.data.dao.base.impl.SuperService;
import com.tennetcn.data.enums.ModelStatus;

/**
 * @author      tancongzhu
 * @email       
 * @createtime  2018年04月24日 10:59:47
 * @comment
 */
@Component
public class StaffServiceImpl extends SuperService<Staff> implements IStaffService{
	@Autowired
	protected ILoginUserService loginUserService;
	@Autowired
	private IShopService shopService;
	@Autowired
	private IRoleLoginUserService roleLoginUserService;
	@Autowired
	private ICustomerService customerService;
	@Autowired
	private IStaffDao staffDao;
	@Override
	public boolean applyStaff(Shop shop, AuthorityLoginUser authorityLoginUser) {
		if(shopService.applyChange(shop)) {
			Staff staff =new Staff(); 
			staff.setId(authorityLoginUser.getId());
			staff.setName(authorityLoginUser.getName());
			staff.setMobile(authorityLoginUser.getAccount());
			staff.setCode(StringUtils.isEmpty(authorityLoginUser.getCode())?ChineseUtils.getPinYin(staff.getName()):authorityLoginUser.getCode());
			staff.setMarkCode(StringUtils.isEmpty(authorityLoginUser.getMarkCode())?ChineseUtils.getPinYinHeadChar(staff.getName()):authorityLoginUser.getMarkCode());
			staff.setEmail(authorityLoginUser.getEmail());
			staff.setSex(StringUtils.isEmpty(staff.getSex())?SexStatus.unknown:authorityLoginUser.getSex());
			staff.setBusinessId(authorityLoginUser.getBusinessId());
			staff.setDeleteMark(YesOrNoInteger.no);
			staff.setCreateDate(staff.getCreateDate()==null?DateUtils.getCurrent():authorityLoginUser.getCreateDate());
			staff.setCreateUserId(authorityLoginUser.getCreateUserId());
			staff.setCreateUserName(authorityLoginUser.getCreateUserName());
			staff.setDeleteMark(YesOrNoInteger.no);
			staff.setShopId(shop.getShopId());
			staff.setModelStatus(ModelStatus.add);
			
			Customer customer=new Customer();
			customer.setCustomerId(CommonUtils.getKeyId());
			customer.setCreateDate(DateUtils.getCurrent());
			customer.setCustomerName("零售客");
			customer.setCustomerTel("无");
			customer.setShopId(shop.getShopId());
			customer.setDeleteMark(YesOrNoInteger.no);
			customer.setModelStatus(ModelStatus.add);
			if(super.applyChange(staff)) {
				return loginUserService.applyLoginUser(authorityLoginUser)&&customerService.applyChange(customer);
			}
			
		}
		return false;
	}

	@Override
	public boolean applyChange(Staff staff, List<Role> roles,String password) {
		List<RoleLoginUser> roleLoginUsers=new ArrayList<RoleLoginUser>();
		if(super.applyChange(staff)){
			AuthorityLoginUser authorityLoginUser=new AuthorityLoginUser();
			authorityLoginUser.setId(staff.getId());
			authorityLoginUser.setAccount(staff.getMobile());
			authorityLoginUser.setName(staff.getName());
			authorityLoginUser.setPassword(password);
			authorityLoginUser.setCreateDate(DateUtils.getCurrent());
			if(staff.getModelStatus() == ModelStatus.add){
				authorityLoginUser.setModelStatus(ModelStatus.add);
			}else {
				if(roleLoginUserService.deleteByLoginUserId(staff.getId())) {
					authorityLoginUser.setModelStatus(ModelStatus.update);
				}
			}
			for(Role role:roles) {
				RoleLoginUser roleLoginUser=new RoleLoginUser();
				roleLoginUser.setId(CommonUtils.getKeyId());
				roleLoginUser.setCreateDate(DateUtils.getCurrent());
				roleLoginUser.setCreateUserId(staff.getCreateUserId());
				roleLoginUser.setCreateUserName(staff.getCreateUserName());
				roleLoginUser.setLoginUserId(staff.getId());
				roleLoginUser.setRoleId(role.getRoleId());
				roleLoginUser.setDeleteMark(YesOrNoInteger.no);
				roleLoginUser.setModelStatus(ModelStatus.add);
				roleLoginUsers.add(roleLoginUser);
			}
			System.err.println(authorityLoginUser.getId());
			System.err.println(authorityLoginUser.getModelStatus());
			return roleLoginUserService.applyChanges(roleLoginUsers)&&loginUserService.applyLoginUser(authorityLoginUser);
		}
		return false;
	}

	@Override
	public boolean applyStaffAndRole(Staff staff) {
		List<RoleLoginUser> roleLoginUsers=new ArrayList<RoleLoginUser>();
		if(super.applyChange(staff)){
			if(ModelStatus.update.equals(staff.getModelStatus())){
				if(roleLoginUserService.deleteByLoginUserId(staff.getId())) {
					
				}
			}
			if(!StringUtils.isEmpty(staff.getRoleIds())) {
				String[] roleIds=staff.getRoleIds().split(",");
				for(int i=0;i<roleIds.length;i++){
					RoleLoginUser roleLoginUser=new RoleLoginUser();
					roleLoginUser.setId(CommonUtils.getKeyId());
					roleLoginUser.setCreateDate(DateUtils.getCurrent());
					roleLoginUser.setCreateUserId(staff.getCreateUserId());
					roleLoginUser.setCreateUserName(staff.getCreateUserName());
					roleLoginUser.setLoginUserId(staff.getId());
					roleLoginUser.setRoleId(roleIds[i]);
					roleLoginUser.setDeleteMark(YesOrNoInteger.no);
					roleLoginUser.setModelStatus(ModelStatus.add);
					roleLoginUsers.add(roleLoginUser);
				}
				return roleLoginUserService.applyChanges(roleLoginUsers);
			}else {
				return true;
			}
		}
		return false;
	}

	@Override
	public boolean checkMobile(String mobile, String id) {
		
		return staffDao.checkMobile(mobile, id);
	}

}